﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace _1065093_1065130_1065120_DoAn
{
    public partial class QuanLyTaiKhoan : UserControl
    {
        public QuanLyTaiKhoan()
        {
            InitializeComponent();
        }

        private void QuanLyTaiKhoan_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            NHANVIENBUS nvBus = new NHANVIENBUS();
            dt = nvBus.LoadDSNhanVien();

            cmbid.DataSource = dt;
            cmbid.DisplayMember = "MaNV";
            cmbid.ValueMember = "MaNV";

            DataTable dt1 = new DataTable();
            LOAINHANVIENBUS lnvBus = new LOAINHANVIENBUS();
            dt1 = lnvBus.LoadDSLoaiNV();

            cmbloaitk.DataSource = dt1;
            cmbloaitk.DisplayMember = "MaLoaiNV";
            cmbloaitk.ValueMember = "MaLoaiNV";

            LoadTK();
        }

        public void LoadTK()
        {
            TAIKHOANBUS tk = new TAIKHOANBUS();
            DataTable dt = new DataTable();
            dt = tk.LoadDSTaiKhoan();
            dgvdanhsachtk.DataSource = dt;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(cmbid.SelectedValue.ToString());
                string matkhau = txtmatkhau.Text;
                int loaitk = Convert.ToInt32(cmbloaitk.SelectedValue.ToString());

                TAIKHOANBUS tkBus = new TAIKHOANBUS();
                TAIKHOANDTO tkDto = new TAIKHOANDTO(id, matkhau, loaitk);
                tkBus.ThemTK(tkDto);
                LoadTK();
                MessageBox.Show("Thành công");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn xoá?","", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int id = Convert.ToInt32(dgvdanhsachtk.CurrentRow.Cells[0].Value);

                    TAIKHOANBUS tkBus = new TAIKHOANBUS();
                    tkBus.XoaTK(id);
                    LoadTK();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void dgvdanhsachtk_SelectionChanged(object sender, EventArgs e)
        {
            cmbid.Text = dgvdanhsachtk.CurrentRow.Cells[0].Value.ToString();
            txtmatkhau.Text = dgvdanhsachtk.CurrentRow.Cells[1].Value.ToString();
            cmbloaitk.Text = dgvdanhsachtk.CurrentRow.Cells[2].Value.ToString();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn sửa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int id = Convert.ToInt32(cmbid.SelectedValue.ToString());
                    string matkhau = txtmatkhau.Text;
                    int loaitk = Convert.ToInt32(cmbloaitk.SelectedValue.ToString());

                    TAIKHOANBUS tkBus = new TAIKHOANBUS();
                    TAIKHOANDTO tkDto = new TAIKHOANDTO(id, matkhau, loaitk);
                    tkBus.SuaTK(tkDto);
                    LoadTK();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("Không có từ khoá cần tìm", "");
                return;
            }
            LoadTim();
        }

        public void LoadTim()
        {
                string tukhoa = txtsearch.Text;
                TAIKHOANBUS tkBus = new TAIKHOANBUS();
                DataTable dt = new DataTable();
                dt = tkBus.LoadDSTimKiem(tukhoa);
                dgvdanhsachtimkiem.DataSource = dt;
        }
    }
}
